﻿// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "UiCustom/DreamRotate"
{
	Properties
	{
		_MainTex("Main Tex", 2D) = "white" {}
		_Rot("Rotation", float) = 0
	}

	SubShader
		{
			Tags{ "Queue" = "Geometry" }
			Pass
			{
				Tags{ "LightMode" = "ForwardBase" }
				ZWrite Off
				Blend SrcAlpha OneMinusSrcAlpha

				CGPROGRAM
				#pragma vertex vert  
				#pragma fragment frag
				#include "UnityCG.cginc"
				#define PI 3.14159265358979  

				sampler2D _MainTex;
				float _Rot;
				struct a2v
				{
					float4 vertex : POSITION;
					float3 texcoord : TEXCOORD0;
				};

				struct v2f
				{
					float4 pos : SV_POSITION;
					float2 uv : TEXCOORD0;
				};

				v2f vert(a2v v)
				{
					v2f o;
					o.pos = UnityObjectToClipPos(v.vertex);
					o.uv = v.texcoord; 
					return o;
				}

				fixed4 frag(v2f i) : SV_Target
				{
					fixed x = i.uv.x - 0.5;
					fixed y = i.uv.y - 0.5;
					half dis = sqrt(x * x + y * y); //与中心点(0.5,0.5)的距离
					//距离越大，旋转角度越大
					half angle = step(i.uv.x, 0.5)*PI + atan(y / x) + _Rot * dis;

					//计算坐标
					fixed cos_angle, sin_angle;
					sincos(angle, sin_angle, cos_angle);
					i.uv.x = 0.5 + dis * cos_angle;
					i.uv.y = 0.5 + dis * sin_angle;
					//旋转变换会出现uv坐标超限的情况，不过添加处理之后效果也一般
//					if (i.uv.y < 0 || i.uv.y > 1 || i.uv.x < 0 || i.uv.x > 1)
//						return fixed4(1, 1, 1, 0);
					return tex2D(_MainTex, i.uv);
				}
				ENDCG
			}
		}
		FallBack "Specular"
}